package com.example.controller;

import com.example.common.Result;
import com.example.common.PageResult;
import com.example.dto.SupplyProductDTO;
import com.example.service.SupplyProductService;
import com.example.service.SupplyCompanyService;
import com.example.service.SupplyCategoryService;
import com.example.vo.SupplyProductVO;
import com.example.vo.SupplyCompanyVO;
import com.example.vo.SupplyCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 供货商品控制器
 */
@RestController
@RequestMapping("/api/supply-product")
@CrossOrigin
public class SupplyProductController {
    
    @Autowired
    private SupplyProductService supplyProductService;
    
    @Autowired
    private SupplyCompanyService supplyCompanyService;
    
    @Autowired
    private SupplyCategoryService supplyCategoryService;
    
    /**
     * 添加供货商品
     */
    @PostMapping
    public Result<Void> addSupplyProduct(@RequestBody SupplyProductDTO supplyProductDTO) {
        return supplyProductService.addSupplyProduct(supplyProductDTO);
    }
    
    /**
     * 删除供货商品
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteSupplyProduct(@PathVariable Long id) {
        return supplyProductService.deleteSupplyProduct(id);
    }
    
    /**
     * 更新供货商品
     */
    @PutMapping("/{id}")
    public Result<Void> updateSupplyProduct(@PathVariable Long id, @RequestBody SupplyProductDTO supplyProductDTO) {
        return supplyProductService.updateSupplyProduct(id, supplyProductDTO);
    }
    
    /**
     * 根据ID查询供货商品
     */
    @GetMapping("/{id}")
    public Result<SupplyProductVO> getSupplyProductById(@PathVariable Long id) {
        return supplyProductService.getSupplyProductById(id);
    }
    
    /**
     * 查询所有供货商品
     */
    @GetMapping("/list")
    public Result<List<SupplyProductVO>> getAllSupplyProducts() {
        return supplyProductService.getAllSupplyProducts();
    }
    
    /**
     * 分页查询供货商品
     */
    @GetMapping("/page")
    public Result<PageResult<SupplyProductVO>> getSupplyProductPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String keyword) {
        return supplyProductService.getSupplyProductPage(page, size, keyword);
    }
    
    /**
     * 检查商品编号是否存在
     */
    @GetMapping("/check-code")
    public Result<Boolean> checkProductCodeExists(
            @RequestParam String productCode,
            @RequestParam(required = false) Long excludeId) {
        return supplyProductService.checkProductCodeExists(productCode, excludeId);
    }
    
    /**
     * 获取所有启用的供货公司
     */
    @GetMapping("/companies")
    public Result<List<SupplyCompanyVO>> getAllEnabledSupplyCompanies() {
        return supplyCompanyService.getAllEnabledSupplyCompanies();
    }
    
    /**
     * 获取所有启用的供货分类
     */
    @GetMapping("/categories")
    public Result<List<SupplyCategoryVO>> getAllEnabledSupplyCategories() {
        return supplyCategoryService.getAllEnabledSupplyCategories();
    }
} 